package com.example.venue_reservation_service.utils;

import cn.hutool.core.lang.UUID;
import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

public class DownExcel {
    public static void download(HttpServletResponse response, Class t, List list, String prefix) throws IOException, IllegalAccessException,InstantiationException {
        String substring = UUID.randomUUID().toString().substring(0, 5);
        response.setContentType("application/vnd.ms-excel");// 设置文本内省
        response.setCharacterEncoding("UTF-8");// 设置字符编码
        response.setHeader("Content-disposition", "attachment;filename="+prefix+"-"+substring+".xlsx"); // 设置响应头
        EasyExcel.write(response.getOutputStream(), t)
                .sheet("数据报表")
                .doWrite(list); //用io流来写入数据
    }
}